package com.wxproject.demo.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxproject.demo.Entity.AO.ArticleCreateAO;
import com.wxproject.demo.Entity.Model.ArticleDO;
import com.wxproject.demo.Entity.VO.*;
import com.wxproject.demo.Mapper.ArticleMapper;
import com.wxproject.demo.Service.ArticleService;
import com.wxproject.demo.Utils.DateUtils;
import com.wxproject.demo.Utils.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author HCJ
 * @date 2021/4/4 19:23
 * @description
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public PageInfo getAllByPage(int page,int size,String typeId) {

        PageHelper.startPage(page,size);
        List<PageArticleVO> list = articleMapper.getAllByPageHelper(typeId);
        PageInfo<PageArticleVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
        /*List<ArticleDO> list = articleMapper.getAll();
        PageInfo<ArticleDO> pageInfo = new PageInfo<>(list);
        return pageInfo;*/

        /*PageHelper.startPage(page,size);
        System.out.println(page+" "+size);
        PageInfo<PageArticleVO> pageInfo = new PageInfo(articleMapper.getAllByPageHelper(typeId));

        return pageInfo;*/

        /*int start;
        if(page==1||page==0){ start=0; }else{ start=(page-1)*size; }
        List<PageArticleVO> allByPage = articleMapper.getAllByPage(start, size, typeId);
        for(PageArticleVO pageArticleVO:allByPage){
            Integer commentCount = articleMapper.getCommentCount(pageArticleVO.getId());
            pageArticleVO.setCommitCount(String.valueOf(commentCount));
        }
        return allByPage;*/
    }

    @Override
    public List<ArticleDO> getAll() {
        return articleMapper.getAll();
    }

    @Override
    public Boolean look(String articleId) {
        if(articleMapper.look(articleId)!=0){
            return true;
        }
        return false;
    }
    @Override
    public Boolean like(String articleId) {
        if(articleMapper.like(articleId)!=0){
            return true;
        }
        return false;
    }
    @Override
    public Boolean dislike(String articleId) {
        if(articleMapper.dislike(articleId)!=0){
            return true;
        }
        return false;
    }
    @Override
    public Boolean logicdeleteArticle(String articleId) {
        if(articleMapper.logicDeleteArticle(articleId)!=0){
            return true;
        }
        return false;
    }
    @Override
    public Boolean DeleteArticle(String articleId) {
        if(articleMapper.deleteArticle(articleId)!=0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean isOwnerOfArticle(String articleId, String userId) {
        if(articleMapper.isOwnerOfArticle(articleId).equals(userId)){
            return true;
        }
        return false;
    }
    @Override
    public Boolean isArticleExists(String articleId) {
        if(articleMapper.isArticleExists(articleId)!=0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean createArticle(ArticleCreateAO articleCreateAO,String userId) {
        //从参数获得 文章标题 内容 用户ID 分类ID 关键词 文章目录 缩略图
        ArticleDO articleDO = new ArticleDO();
        articleDO.setTypeId("8069124127937572865");
        BeanUtils.copyProperties(articleCreateAO,articleDO);
        //用户ID
        articleDO.setUserId(userId);
        //雪花算法生成唯一ID
        articleDO.setId(String.valueOf(SnowFlake.nextId()));
        //获取系统时间作为创建时间
        String date = DateUtils.getDate();
        articleDO.setCreateTime(date.substring(0, 16));
        //阅读量 点赞量 踩量 初始化为零
        articleDO.setLikeCount("0");
        articleDO.setDislikeCount("0");
        articleDO.setReadCount("0");
        //推荐等级需要综合分析生成 默认为1
        articleDO.setRecommendLevel("1");
        //逻辑删除默认为0
        articleDO.setIsDelete("0");
        if(articleMapper.createArticle(articleDO)!=0){
            return true;
        }
        return false;
    }

    @Override
    public List<RecommendArticleVO> recommendedArticle() {
        String date = DateUtils.getDate().substring(0, 10);
        StringBuilder stringBuilder = new StringBuilder(date);
        String today = stringBuilder.append("%").toString();
        List<RecommendArticleVO> recommendArticleVO = articleMapper.recommendedArticle(today);
        return recommendArticleVO;
    }

    @Override
    public ArticleDetailsVO getArticleDetails(String articleId) {
        ArticleDetailsVO articleDetails = articleMapper.getArticleDetails(articleId);
        return articleDetails;
    }
    @Override
    public ArticleContextVO getArticleContext(String id) {
        ArticleContextVO articleContextVO;
        articleContextVO = articleMapper.getContext(id);
        List<OneCommentVO> res = articleMapper.getComment(articleContextVO.getId());
        articleContextVO.setOneComment(res);
        return articleContextVO;
    }

//    @Override
//    public ArticleContentVO getArticleContext(String id) {
//        ArticleContentVO articleContentVO;
//        articleContentVO = articleMapper.getArticleContext(id);
//        List<ArticleOneCommentVO> articleOneComment = articleMapper.getOneComment(id);
//        List<ArticleTwoCommentVO> articleTwoCommentVO;
//        for (int i = 0; i <articleOneComment.size() ; i++) {
//             articleTwoCommentVO = articleMapper.getTwoComment(articleOneComment.get(i).getId());
//             articleOneComment.get(i).setCommentTwo(articleTwoCommentVO);
//        }
//        articleContentVO.setComment(articleOneComment);
//        return articleContentVO;
//    }

    @Override
    public PageInfo serchArticles(int page, int size, String key) {
        PageHelper.startPage(page,size);
        List<PageArticleVO> list = articleMapper.serchArticles(key);
        PageInfo<PageArticleVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
